import create from '../../utils/create2.js'
create({
  /**
   * 组件的属性列表
   */
  properties: {
    name: {
      type: String,
      value: ''
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    bySaleIcon: '../../pages/images/sp_05.png',
    byPriceIcon: '../../pages/images/sp_05.png',
    filterIcon: '../../pages/images/sp_07.png',
    orderBy: '',
    goodsFilter: {}
  },
  pageLifetimes: {
    show() {
      const { goodsFilter } = this.data
      if (Object.keys(goodsFilter).length > 0) {
        this.setData({
          orderBy: 'filter',
          filterIcon: '../../pages/images/sp_08.png',
          bySaleIcon: '../../pages/images/sp_05.png',
          byPriceIcon: '../../pages/images/sp_05.png'
        })
      }
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    OrderBy(e) {
      const { type } = e.currentTarget.dataset
      if (type === 'sale') {
        this.setData({
          byPriceIcon: '../../pages/images/sp_05.png'
        })
      } else {
        this.setData({
          bySaleIcon: '../../pages/images/sp_05.png'
        })
      }
      const { bySaleIcon, byPriceIcon } = this.data
      let icon = ''
      let sort = 0
      let curr = type
      if (type === 'sale') {
        if (bySaleIcon === '../../pages/images/sp_05.png') {
          icon = '../../pages/images/sp_02.png'
          sort = 2
        } else if (bySaleIcon === '../../pages/images/sp_02.png') {
          icon = '../../pages/images/sp_03.png'
          sort = 1
        } else {
          icon = '../../pages/images/sp_05.png'
          sort = 0
          curr = ''
        }
      } else if (type === 'price') {
        if (byPriceIcon === '../../pages/images/sp_05.png') {
          icon = '../../pages/images/sp_02.png'
          sort = 4
        } else if (byPriceIcon === '../../pages/images/sp_02.png') {
          icon = '../../pages/images/sp_03.png'
          sort = 3
        } else {
          icon = '../../pages/images/sp_05.png'
          sort = 0
          curr = ''
        }
      }
      this.setData({
        orderBy: type,
        bySaleIcon: type === 'sale' ? icon : bySaleIcon,
        byPriceIcon: type === 'price' ? icon : byPriceIcon,
        filterIcon: '../../pages/images/sp_07.png',
        orderBy: curr
      })
      this.store.data.goodsFilter = {}
      this.update()
      this.triggerEvent("Search", { type: sort })
    },
    goFilter() {
      wx.navigateTo({
        url: '/pages/product/filter/filter?from=1'
      })
    },
    updateSearchKey: function (e) {
      this.setData({
        name: e.detail.value
      })
    },
    goSearchView() {
      wx.navigateTo({
        url: '../../search/index?from=1',
      })
    },
    goSearch(){
      const {name} = this.data
      this.triggerEvent('Search', name)
    }
  }
})